package com.zhdl.modules.wallpulley.service.impl;

import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.wallpulley.entity.EventRecords;
import com.zhdl.modules.wallpulley.entity.MeasurementData;
import com.zhdl.modules.wallpulley.entity.ParameterSettings;
import com.zhdl.modules.wallpulley.entity.TensileDistance;
import com.zhdl.modules.wallpulley.repository.EventRecordsDao;
import com.zhdl.modules.wallpulley.repository.MeasurementDataDao;
import com.zhdl.modules.wallpulley.repository.ParameterSettingsDao;
import com.zhdl.modules.wallpulley.repository.TensileDistanceDao;
import com.zhdl.modules.wallpulley.service.TensileDistanceService;
import com.zhdl.modules.wallpulley.web.resp.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class TensileDistanceServiceImpl extends BaseServiceImpl<Integer, EventRecords, EventRecordsDao> implements TensileDistanceService {

    @Resource
    private EventRecordsDao eventRecordsDao;

    @Resource
    private MeasurementDataDao measurementDataDao;

    @Resource
    private ParameterSettingsDao parameterSettingsDao;

    @Resource
    private TensileDistanceDao tensileDistanceDao;

    private static List<TensileDistanceResp> getTensileDistanceResps(List<TensileDistance> tensileDistancesByMeasurementId) {
        List<TensileDistanceResp> respList = new ArrayList<>();
        for (TensileDistance tensileDistance : tensileDistancesByMeasurementId) {
            TensileDistanceResp tensileDistanceResp = new TensileDistanceResp();
            tensileDistanceResp.setDistanceValue(tensileDistance.getDataDistance());
            tensileDistanceResp.setTensileValue(tensileDistance.getDataKilogram());
            tensileDistanceResp.setType(tensileDistance.getDataType());

            tensileDistanceResp.setDataPointA(tensileDistance.getDataPoint1());
            tensileDistanceResp.setDataPointB(tensileDistance.getDataPoint2());
            tensileDistanceResp.setDataPointC(tensileDistance.getDataPoint3());

            tensileDistanceResp.setDatePointAChange(tensileDistance.getDatePointAChange());
            tensileDistanceResp.setDatePointBChange(tensileDistance.getDatePointBChange());
            tensileDistanceResp.setDatePointCChange(tensileDistance.getDatePointCChange());


            tensileDistanceResp.setNowTime(tensileDistance.getCreateDate());
            tensileDistanceResp.setNowLongTime(tensileDistance.getNowLongTime());
            respList.add(tensileDistanceResp);
        }
        return respList;
    }

    @Override
    public List<EventRecords> findAllEventRecords() {
        return eventRecordsDao.findAll();
    }

    @Override
    public List<TensileDistance> findAllTensileDistance() {
        return tensileDistanceDao.findAll();
    }

    @Override
    public List<ParameterSettings> findAllParameterSettings() {
        return parameterSettingsDao.findAll();
    }

    @Override
    public List<MeasurementData> findAllMeasurementData() {
        return measurementDataDao.findAll();
    }

    @Override
    public ParameterSettings findParameterById(int id) {
        Optional<ParameterSettings> byId = parameterSettingsDao.findById(id);
        ParameterSettings parameterSettings = byId.get();
        return parameterSettings;
    }

    @Override
    public TensileDistance findTensileDistanceById(int id) {
        Optional<TensileDistance> byId = tensileDistanceDao.findById(id);
        return byId.get();
    }

    @Override
    public EventRecords findEventRecordsById(int id) {
        Optional<EventRecords> byId = eventRecordsDao.findById(id);
        return byId.get();
    }

    @Override
    public MeasurementData findMeasurementDataById(int id) {
        Optional<MeasurementData> byId = measurementDataDao.findById(id);
        return byId.get();
    }

    @Override
    public int updateEventRecords(EventRecords resp) {
        EventRecords save = eventRecordsDao.save(resp);
        return save.getId();
    }

    @Override
    public int saveEventRecords(EventRecordsResp resp) {
        EventRecords eventRecords = new EventRecords();
        eventRecords.setModelId(resp.getModelId());
        eventRecords.setRecordResult(resp.getRecordResult());
        EventRecords save = eventRecordsDao.save(eventRecords);
        return save.getId();
    }

    @Override
    public int saveMeasurementData(MeasurementDataResp resp) {
        MeasurementData measurement = new MeasurementData();
        measurement.setEventId(resp.getEventId());
        measurement.setKilogramAverage(resp.getKilogramAverage());
        measurement.setKilogramMin(resp.getKilogramMin());
        measurement.setKilogramMax(resp.getKilogramMax());
        measurement.setProductQualified(resp.getProductQualified());

        measurement.setL1ValueNo1(resp.getL1ValueNo1());
        measurement.setL2ValueNo1(resp.getL2ValueNo1());
        measurement.setL3ValueNo1(resp.getL3ValueNo1());
        measurement.setL1ValueNo2(resp.getL1ValueNo2());
        measurement.setL2ValueNo2(resp.getL2ValueNo2());
        measurement.setL3ValueNo2(resp.getL3ValueNo2());
        measurement.setL1ValueNo3(resp.getL1ValueNo3());
        measurement.setL2ValueNo3(resp.getL2ValueNo3());
        measurement.setL3ValueNo3(resp.getL3ValueNo3());
        measurement.setL1ValueNo4(resp.getL1ValueNo4());
        measurement.setL2ValueNo4(resp.getL2ValueNo4());
        measurement.setL3ValueNo4(resp.getL3ValueNo4());


        measurement.setNo_on1(resp.getNo_on1());
        measurement.setNo_on2(resp.getNo_on2());
        measurement.setNo_on3(resp.getNo_on3());
        measurement.setNo_on4(resp.getNo_on4());
        measurement.setNo_off1(resp.getNo_off1());
        measurement.setNo_off2(resp.getNo_off2());
        measurement.setNo_off3(resp.getNo_off3());
        measurement.setNo_off4(resp.getNo_off4());
        MeasurementData save = measurementDataDao.save(measurement);
        return save.getId();
    }

    @Override
    public void saveParameterSetting(ParameterSettingsResp resp) {
        Optional<ParameterSettings> byId = parameterSettingsDao.findById(resp.getPsId());
        ParameterSettings parameterSettings = byId.get();
        parameterSettings.setModelEmpty(resp.getModelEmpty());
        parameterSettings.setModelSpeed(resp.getModelSpeed());
        parameterSettings.setModelDistance(resp.getModelDistance());
        parameterSettings.setModelName(resp.getModelName());
        parameterSettings.setModelZero(resp.getModelZero());
        parameterSettingsDao.save(parameterSettings);
    }

    @Override
    public void saveTensileDistance(List<TensileDistanceResp> resps) {
        for (TensileDistanceResp resp : resps) {
            TensileDistance tensileDistance = new TensileDistance();
            tensileDistance.setMeasurementId(resp.getMeasurementId());
            tensileDistance.setWorkOrderNumber(resp.getWorkOrderNumber());
            tensileDistance.setDataDistance(resp.getDistanceValue());
            tensileDistance.setDataKilogram(resp.getTensileValue());
            tensileDistance.setDataType(resp.type);

            tensileDistance.setDataPoint1(resp.getDataPointA());
            tensileDistance.setDataPoint2(resp.getDataPointB());
            tensileDistance.setDataPoint3(resp.getDataPointC());

            tensileDistance.setDatePointAChange(resp.getDatePointAChange());
            tensileDistance.setDatePointBChange(resp.getDatePointBChange());
            tensileDistance.setDatePointCChange(resp.getDatePointCChange());

            tensileDistance.setNowLongTime(resp.getNowLongTime());
            tensileDistance.setCreateDate(resp.getNowTime());
            tensileDistanceDao.save(tensileDistance);
        }
    }

    @Override
    public EventInfoResp findInfoByEventId(int eventId) {
        MeasurementData measurementDataByEventId = measurementDataDao.findMeasurementDataByEventId(eventId);
        EventInfoResp eventInfoResp = new EventInfoResp();
        eventInfoResp.setMin(measurementDataByEventId.getKilogramMin());
        eventInfoResp.setMax(measurementDataByEventId.getKilogramMax());
        eventInfoResp.setAverage(measurementDataByEventId.getKilogramAverage());
        eventInfoResp.setResult(measurementDataByEventId.getProductQualified());

        eventInfoResp.setL1ValueNo1(measurementDataByEventId.getL1ValueNo1());
        eventInfoResp.setL1ValueNo2(measurementDataByEventId.getL1ValueNo2());
        eventInfoResp.setL1ValueNo3(measurementDataByEventId.getL1ValueNo3());
        eventInfoResp.setL1ValueNo4(measurementDataByEventId.getL1ValueNo4());
        eventInfoResp.setL2ValueNo1(measurementDataByEventId.getL2ValueNo1());
        eventInfoResp.setL2ValueNo2(measurementDataByEventId.getL2ValueNo2());
        eventInfoResp.setL2ValueNo3(measurementDataByEventId.getL2ValueNo3());
        eventInfoResp.setL2ValueNo4(measurementDataByEventId.getL2ValueNo4());
        eventInfoResp.setL3ValueNo1(measurementDataByEventId.getL3ValueNo1());
        eventInfoResp.setL3ValueNo2(measurementDataByEventId.getL3ValueNo2());
        eventInfoResp.setL3ValueNo3(measurementDataByEventId.getL3ValueNo3());
        eventInfoResp.setL3ValueNo4(measurementDataByEventId.getL3ValueNo4());

        eventInfoResp.setNo_on1(measurementDataByEventId.getNo_on1());
        eventInfoResp.setNo_on2(measurementDataByEventId.getNo_on2());
        eventInfoResp.setNo_on3(measurementDataByEventId.getNo_on3());
        eventInfoResp.setNo_on4(measurementDataByEventId.getNo_on4());
        eventInfoResp.setNo_off1(measurementDataByEventId.getNo_off1());
        eventInfoResp.setNo_off2(measurementDataByEventId.getNo_off2());
        eventInfoResp.setNo_off3(measurementDataByEventId.getNo_off3());
        eventInfoResp.setNo_off4(measurementDataByEventId.getNo_off4());
        Integer mdId = measurementDataByEventId.getId();
        List<TensileDistance> tensileDistancesByMeasurementId = tensileDistanceDao.findTensileDistancesByMeasurementId(mdId);
        List<TensileDistanceResp> respList = getTensileDistanceResps(tensileDistancesByMeasurementId);
        eventInfoResp.setRespList(respList);
        return eventInfoResp;
    }
}
